/*
*********************************************************************************************************
*                                               uC/OS-II
*                                         The Real-Time Kernel
*
*                             (c) Copyright 2007, Micrium, Inc., Weston, FL
*                                          All Rights Reserved
*
*
*                                              Sample code
*
* File : APP.C
* By   : Hong Soong
*********************************************************************************************************
*/

#include <include.h>

/*
*********************************************************************************************************
*                                                VARIABLES
*********************************************************************************************************
*/
        
static  OS_STK      AppTaskStartStk[APP_START_TASK_STK_SIZE];
static  OS_STK      AppTask1Stk[APP_TASK1_STK_SIZE];

/*
*********************************************************************************************************
*                                            FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void  AppTaskCreate (void);
static  void  AppTaskStart  (void *p_arg);
static  void  AppTask1      (void *p_arg);

#if (uC_PROBE_OS_PLUGIN > 0) || (uC_PROBE_COM_MODULE > 0)
extern  void  AppProbeInit  (void);
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                                main()
*
* Description : This is the standard entry point for C code.  It is assumed that your code will call
*               main() once you have performed all necessary CPU and C initialization.
*
* Arguments   : none
*********************************************************************************************************
*/

void  main (void)
{
    CPU_INT08U  err;

    set_imask(15);
    //BSP_IntDisAll();                                                    /* Disable all interrupts until we are ready to accept them */

    OSInit();                                                           /* Initialize "uC/OS-II, The Real-Time Kernel"              */

    OSTaskCreateExt(AppTaskStart,                                       /* Create the start task                                    */
                    (void *)0,
                    (OS_STK *)&AppTaskStartStk[APP_START_TASK_STK_SIZE - 1],
                    APP_START_TASK_PRIO,
                    APP_START_TASK_PRIO,
                    (OS_STK *)&AppTaskStartStk[0],
                    APP_START_TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);

#if OS_TASK_NAME_EN > 0
    OSTaskNameSet(APP_START_TASK_PRIO, (CPU_CHAR *)"Startup", &err);
#endif

    OSStart();                                                          /* Start multitasking (i.e. give control to uC/OS-II)       */
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                          STARTUP TASK
*
* Description : This is an example of a startup task.  As mentioned in the book's text, you MUST
*               initialize the ticker only once multitasking has started.
*
* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'.
*
* Note(s)     : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not
*                  used.  The compiler should not generate any code for this statement.
*               2) Interrupts are enabled once the task start because the I-bit of the CCR register was
*                  set to 0 by 'OSTaskCreate()'.
*********************************************************************************************************
*/

static  void  AppTaskStart (void *p_arg)
{
    CPU_INT08U  i;


    (void)p_arg;

    BSP_Init();                                                         /* Initialize BSP functions                                 */
    
#if (OS_TASK_STAT_EN > 0)
    OSStatInit();                                                       /* Determine CPU capacity                                   */
#endif

    //Mem_Init();                                                         /* Initialize mem mgmt module, necessary for TCP-IP.        */

#if (PROBE_COM_CFG_RS232_EN == DEF_DISABLED)
    //SP_Init(BSP_SP_PORT_NUM,                                            /* Initialize serial port                                   */
            //BSP_SP_BAUD_RATE);
#endif

#if (uC_PROBE_OS_PLUGIN > 0) || (uC_PROBE_COM_MODULE > 0)
    //AppProbeInit();                                                     /* Initialize uC/Probe modules                              */
#endif

    //LED_Off(6);                                                         /* Turn OFF all the LEDs                                    */

    AppTaskCreate();                                                    /* Create application tasks                                 */
    
    while (1) 
    {                                                         /* Task body, always written as an infinite loop.           */
        for (i = 0; i < 6; i++) 
        {
            //LED_Toggle(i); 
            
            OSTimeDlyHMSM(0, 0, 0, 200);
        }       
         
        for (i; i > 0; i--) 
        {
            //LED_Toggle(i - 1);

            OSTimeDlyHMSM(0, 0, 0, 200);
        }        
        
        for (i = 0; i < 9; i++) 
        {
            //LED_Toggle(0);
            //LED_Toggle(1);
            //LED_Toggle(2);
            //LED_Toggle(3);
            //LED_Toggle(4);
            //LED_Toggle(5);
                
            OSTimeDlyHMSM(0, 0, 0, 200);
        }
    }
}


/*$PAGE*/
/*
*********************************************************************************************************
*                                        CREATE APPLICATION TASKS
*
* Description: This function creates the application tasks.
*
* Arguments  : none.
*
* Note(s)    : none.
*********************************************************************************************************
*/

static  void  AppTaskCreate (void)
{
    CPU_INT08U  err;
    
    
    OSTaskCreateExt(AppTask1,                                           /* Create start task 1                                      */
                   (void *)0,
                   (OS_STK *)&AppTask1Stk[APP_TASK1_STK_SIZE - 1],
                    APP_START_TASK1_PRIO,
                    APP_START_TASK1_PRIO,
                   (OS_STK *)&AppTask1Stk[0],
                    APP_TASK1_STK_SIZE,
                   (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
                    
#if OS_TASK_NAME_EN > 0
    OSTaskNameSet(APP_START_TASK1_PRIO, (CPU_CHAR *)"Task 1", &err);
#endif
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                          STARTUP TASK 1
*
* Description : This is an example of a startup task. This task will print to the debug LCD.
*
* Arguments   : p_arg   is the argument passed to 'AppTask1()' by 'OSTaskCreate()'.
*
* Returns     : none
*********************************************************************************************************
*/

void  AppTask1 (void *p_arg)
{
    (void)p_arg;
    
    while (1) 
    {    
                                                                        /* Add your code here                                       */
        pwmSetValue[0].pwmCycle = 1000;
        pwmSetValue[0].pwmDuty  = 5000;
        pwmSetValue[0].pwmStart = 1;
        pwmSetValue[0].channel  = 1;
        AuxPwmUpdata( &pwmSetValue[0] );
        OSTimeDlyHMSM(0,0,3,0);
    }
}

